package com.fpmsa.officeweb.controller;

import com.alibaba.fastjson.JSON;
import com.fpmsa.officeweb.constant.OWConstant;
import com.fpmsa.officeweb.core.base.controller.BaseController;
import com.fpmsa.officeweb.core.util.ToolUtil;
import com.fpmsa.officeweb.core.util.UserAgentUtil;
import com.fpmsa.officeweb.model.News;
import com.fpmsa.officeweb.service.INewsCategoryService;
import com.fpmsa.officeweb.service.INewsService;
import com.fpmsa.officeweb.service.INewsThemeService;
import com.fpmsa.officeweb.service.OfficeWebService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Auther: Huang Yuehong
 * @Description:
 * @Date: 2018/6/20 10:30
 */
@Controller
public class OfficeWebController extends BaseController {

    private String PREFIX = "/templates/";

    @Autowired
    private INewsService newsService;

    @Autowired
    private INewsCategoryService newsCategoryService;

    @Autowired
    private INewsThemeService newsThemeService;

    @Autowired
    private OfficeWebService officeWebService;

    /**
     * 跳转到官网首页
     */
    @RequestMapping("/")
    public String index(HttpServletRequest request, Model model) {

        // 移动端发送的请求，是的话直接重定向到移动端的导航页
        if (UserAgentUtil.isMobile(request) || UserAgentUtil.isWechat(request)){
            return "redirect:/nav/index";
        }

        // 获取"协会新闻"类别下“置顶”状态的一条新闻,没有置顶的新闻则直接选用最新的一条
        News topNews = officeWebService.getTopNewsByC(OWConstant.OW_ASSOCIATION_NEWS);
        if (topNews == null){
            List<News> list = officeWebService.getNumNewsByCategory(OWConstant.OW_ASSOCIATION_NEWS,1,false);
            if (ToolUtil.isNotEmpty(list)){
                topNews = list.get(0);
            }
        }
        model.addAttribute("topNews",topNews);
        if (topNews != null){
            model.addAttribute("datetime",new SimpleDateFormat("yyyy-MM-dd").format(topNews.getCreateTime()));
        }

        // “协会新闻”列表（5条）
        List<News> newsList = officeWebService.getNumNewsByCategory(OWConstant.OW_ASSOCIATION_NEWS,5,false);
        // 格式化creat_time为yyyy/MM/dd
        List<Map<String,Object>> newsMapList = officeWebService.formatCreateTime(newsList,"yyyy/MM/dd");
        model.addAttribute("newsMapList",newsMapList);

        // 获取一条最新的视频信息传到前台
        List<News> vedioNews = officeWebService.getNumNewsByCategory(OWConstant.OW_VEDIO_CENTER,1,false);
        List<Map<String,Object>> vedioMapList = officeWebService.formatCreateTime(vedioNews,"yyyy/MM/dd");
        model.addAttribute("vedioMap",vedioMapList.get(0));

        // 往页面的model对象里面添加“关键词”、“描述”、“title”、下拉框选项等属性
        officeWebService.addAttrForModel(request,model);

        return PREFIX + "index.html";
    }

    /**
     * 跳转到官网首页
     */
    @RequestMapping("/windex")
    public String windex(HttpServletRequest request,Model model) {

        // 获取"协会新闻"类别下“置顶”状态的一条新闻,没有置顶的新闻则直接选用最新的一条
        News topNews = officeWebService.getTopNewsByC(OWConstant.OW_ASSOCIATION_NEWS);
        if (topNews == null){
            List<News> list = officeWebService.getNumNewsByCategory(OWConstant.OW_ASSOCIATION_NEWS,1,false);
            if (ToolUtil.isNotEmpty(list)){
                topNews = list.get(0);
            }
        }
        model.addAttribute("topNews",topNews);
        if (topNews != null){
            model.addAttribute("datetime",new SimpleDateFormat("yyyy-MM-dd").format(topNews.getCreateTime()));
        }

        // “协会新闻”列表（5条）
        List<News> newsList = officeWebService.getNumNewsByCategory(OWConstant.OW_ASSOCIATION_NEWS,5,false);
        // 格式化creat_time为yyyy/MM/dd
        List<Map<String,Object>> newsMapList = officeWebService.formatCreateTime(newsList,"yyyy/MM/dd");
        model.addAttribute("newsMapList",newsMapList);

        // 获取一条最新的视频信息传到前台
        List<News> vedioNews = officeWebService.getNumNewsByCategory(OWConstant.OW_VEDIO_CENTER,1,false);
        List<Map<String,Object>> vedioMapList = officeWebService.formatCreateTime(vedioNews,"yyyy/MM/dd");
        model.addAttribute("vedioMap",vedioMapList.get(0));

        // 往页面的model对象里面添加“关键词”、“描述”、“title”、下拉框选项等属性
        officeWebService.addAttrForModel(request,model);

        return PREFIX + "index.html";
    }

    /**
     * 首页左下的各类新闻列表动态加载相关方法
     */
    @RequestMapping("/webnews/loadnbody/{index}")
    @ResponseBody
    public String loadnbody(@PathVariable("index") Integer index){
        // 存放返回结果的map
        HashMap<String,Object> map = new HashMap<>();

        // 2代表“媒体报道”类别
        if (index == 2){
            // 获取"媒体报道"类别下“置顶”状态的一条新闻,没有置顶的新闻则直接选用最新的一条
            News topNews = officeWebService.getTopNewsByC(OWConstant.OW_MEDIA_REPORT);
            if (topNews == null){
                List<News> list = officeWebService.getNumNewsByCategory(OWConstant.OW_MEDIA_REPORT,1,false);
                if (ToolUtil.isNotEmpty(list)){
                    topNews = list.get(0);
                }
            }
            map.put("topNews",topNews);
            if (topNews != null){
                map.put("datetime",new SimpleDateFormat("yyyy-MM-dd").format(topNews.getCreateTime()));
            }

            // “协会新闻”列表（5条）
            List<News> newsList = officeWebService.getNumNewsByCategory(OWConstant.OW_MEDIA_REPORT,5,false);
            // 格式化creat_time为yyyy/MM/dd
            List<Map<String,Object>> newsMapList = officeWebService.formatCreateTime(newsList,"yyyy/MM/dd");
            map.put("newsMapList",newsMapList);

        }else if (index == 3){// 3:通知公告
            // “通知公告”列表（10条）
            List<News> newsList = officeWebService.getNumNewsByCategory(OWConstant.OW_ANOUNCEMENT,10,false);
            // 格式化creat_time为yyyy/MM/dd
            List<Map<String,Object>> newsMapList = officeWebService.formatCreateTime(newsList,"yyyy/MM/dd");
            map.put("newsMapList",newsMapList);
        }
        return JSON.toJSONString(map);
    }


    /**
     * 官网首页的“搜索”功能（按标题名称模糊匹配搜索）
     */
    @RequestMapping("/search/{condition}")
    public String search(@PathVariable("condition") String condition,HttpServletRequest request,Model model){

        // "浏览更多"时使用
        model.addAttribute("condition",condition);

        // 根据搜索的条件模糊搜索新闻列表
        List<News> searchedNewsList = officeWebService.getSearchedNews(condition,OWConstant.OW_SHOW_NEWS_NUM);

        Integer countNews = officeWebService.getSearchedNews(condition,null).size();
        if (countNews > OWConstant.OW_SHOW_NEWS_NUM){
            model.addAttribute("isShowMore",1);//表示页面显示“点击查看更多”按钮
        }else{
            model.addAttribute("isShowMore",0);//不显示
        }

        // 关键字增加字体红色样式
        for (News news : searchedNewsList){
            news.setTitle(news.getTitle().replace(condition,"<font color='red'>"+condition+"</font>"));
        }

        // 转换searchedNewsList中create_time字段的格式为yyyy/MM/dd
        List<Map<String,Object>> searchedMapList = officeWebService.formatCreateTime(searchedNewsList,"yyyy-MM-dd");
        model.addAttribute("searchedMapList",searchedMapList);

        // 往页面的model对象里面添加“关键词”、“描述”、“title”、下拉框选项等属性,用request判断请求的来源（pc/移动端/微信端）
        officeWebService.addAttrForModel(request,model);

        return PREFIX + "webnews/search_news.html";
    }


    /* *//**
     * 获取“关于协会”下拉菜单
     *//*
    @RequestMapping(value="/menu/aboutCategoryMapsJson")
    @ResponseBody
    public String aboutCategoryMapsJson(){
        return officeWebService.aboutCategoryMapsJson();
    }
    *//**
     * 获取“新闻资讯”下拉菜单
     *//*
    @RequestMapping(value="/menu/newsCategoryMapsJson")
    @ResponseBody
    public String newsCategoryMapsJson(){
        return officeWebService.newsCategoryMapsJson();
    }
    *//**
     * 获取“新时代经理”下拉菜单
     *//*
    @RequestMapping(value="/menu/npmesCategoryMapsJson")
    @ResponseBody
    public String npmesCategoryMapsJson(){
        return officeWebService.npmesCategoryMapsJson();
    }
    *//**
     * 获取“专家团队”下拉菜单
     *//*
    @RequestMapping(value="/menu/teamCategoryMapsJson")
    @ResponseBody
    public String teamCategoryMapsJson(){
        return officeWebService.teamCategoryMapsJson();
    }
    *//**
     * 获取“党建一家”下拉菜单
     *//*
    @RequestMapping(value="/menu/partyCategoryMapsJson")
    @ResponseBody
    public String partyCategoryMapsJson(){
        return officeWebService.partyCategoryMapsJson();
    }
    *//**
     * 获取“会员中心”下拉菜单
     *//*
    @RequestMapping(value="/menu/memberCategoryMapsJson")
    @ResponseBody
    public String memberCategoryMapsJson(){
        return officeWebService.memberCategoryMapsJson();
    }*/

}
